﻿using System.Collections;
using System.Collections.Generic;
using XLua;
using UnityEditor;
using System.IO;
using UnityEngine;
public class LuaManager 
{
    public static LuaEnv luaEnv;
    public static void AddLuaEnv()
    {
        luaEnv = new LuaEnv();
        luaEnv.AddLoader(MyLoader);
    }
    private static byte[] MyLoader(ref string path)
    {
#if !UNITY_EDITOR || AssetBundle
        string abName = path.Split('.')[0];
        string assetPath = path.Replace(abName+".", "").Replace(".", "/");
        TextAsset textAsset= AssetBundleManager.LoadAsset<TextAsset>(abName, assetPath);
        string str= textAsset.text;
        return textAsset.bytes;
#else
        path = path.Replace('.', '/');
        string newPath = Application.dataPath + "/LuaScript/module/" + path + ".txt";
        if (!File.Exists(newPath))
        {
            Log.LogError("lua代码require路径错误----------》" + path + "《");
            return null;
        }
        byte[] bytes = File.ReadAllBytes(newPath);
        return bytes;
#endif
    }
    public static void DestoryLuaEnv()
    {
        luaEnv.Global.Dispose();
        luaEnv.GC();
        luaEnv.Tick();
        luaEnv = null;
        Log.LogError("------Lua虚拟机已消除------");
    }
}
